package com.cq.hd.order.render.impl;

import com.cq.hd.common.enums.biz.BusinessPointsMallStateEnum;
import com.cq.hd.common.enums.biz.OwnStateEnum;
import com.cq.hd.member.api.TbBusinessApi;
import com.cq.hd.member.api.TbBusinessMerchantApi;
import com.cq.hd.member.api.TbBusinessPointsMallApi;
import com.cq.hd.member.api.vo.BusinessMerchantVo;
import com.cq.hd.member.api.vo.BusinessPageVo;
import com.cq.hd.member.api.vo.BusinessPointsMallSettingsVo;
import com.cq.hd.order.render.OrderContext;
import com.cq.hd.order.render.OrderRenderStepEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 商家设置
 *
 * @author much
 * @since 2024/09/13 23:21
 */
@Component
public class BusinessSettingRender extends AbstractRenderStep {

    @Resource
    private TbBusinessApi businessApi;
    @Resource
    private TbBusinessPointsMallApi businessPointsMallApi;
    @Resource
    private TbBusinessMerchantApi businessMerchantApi;

    @Override
    public OrderRenderStepEnum step() {
        return OrderRenderStepEnum.BUSINESS_SETTING;
    }

    @Override
    public void render(OrderContext orderContext) {
        Long businessId = orderContext.getActiveVo().getBusinessId();
        BusinessPointsMallSettingsVo pointsMallSettingsVo = getPointsMallSettings(businessId);
        orderContext.setBusinessPointsMallSettingsVo(pointsMallSettingsVo);

        // 查询商家数据，获取商家自有商户id
        BusinessMerchantVo businessMerchantVo = businessMerchantApi.getOwnByBusinessId(businessId, OwnStateEnum.YES.getValue()).unpack();
        Long merchantId = businessMerchantVo.getMerchantId();
        orderContext.setBusinessMerchantId(merchantId);
    }

    public BusinessPointsMallSettingsVo getPointsMallSettings(Long businessId) {
        BusinessPointsMallSettingsVo pointsMallSettingsVo = new BusinessPointsMallSettingsVo();

        BusinessPageVo businessPageVo = businessApi.getById(businessId).unpack();
        if (businessPageVo == null || !businessPageVo.getPointsMallState().equals(BusinessPointsMallStateEnum.OPEN.getValue())) {
            return pointsMallSettingsVo;
        }

        return businessPointsMallApi.getSettings(businessId).unpack();
    }

}
